package com.inspire.ijc.agent.asminstru;

import com.inspire.ijc.agent.utils.ASMUtiils;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.commons.AdviceAdapter;

/**
 * @author ：inspire
 * @date ：Created in 2023/4/13 10:30
 * @description：
 * @modified By：inspire
 * @version:
 */
public class ProbeMethodVisitor extends AdviceAdapter {
    int access;
    private String methodName;
    String desc;
    String className;
    int methodId;

    protected ProbeMethodVisitor(int api, MethodVisitor mv, int access, String methodName, String desc, String className,int methodId) {
        super(api, mv, access, methodName, desc);
        this.access=access;
        this.methodName = methodName;
        this.desc=desc;
        this.className=className;
        this.methodId=methodId;
    }

    @Override
    protected void onMethodEnter() {
        if(mv!=null){
            String desc2 = desc.substring(0, desc.lastIndexOf(")") + 1);
            ASMUtiils.visitInsn(mv,methodId);
            mv.visitLdcInsn(className);
            mv.visitLdcInsn(methodName+":"+desc2);
            mv.visitMethodInsn(INVOKESTATIC, "com/inspire/ijc/agent/storage/IStack", "push", "(ILjava/lang/String;Ljava/lang/String;)V", false);
            mv.visitEnd();
        }
    }

    @Override
    protected void onMethodExit(int opcode) {
        if(mv!=null){
            mv.visitMethodInsn(INVOKESTATIC, "com/inspire/ijc/agent/storage/IStack", "pop", "()V", false);
//			mv.visitMaxs(2, 0);
            mv.visitEnd();
        }

    }

    @Override
    public void visitEnd() {
        if (mv != null) {
            mv.visitEnd();
        }
    }
}
